// pages/toPersonal/toPersonal.js
const http = require('../../utils/http.js');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
     statusBarHeight: app.globalData.statusBarHeight, //获取手机状态栏高度
    navId: 0,
    infor:{},
    list: [],
    topicList: [],
    isMe:false,
    // showText: false,
  },

  toBack(){
    wx.navigateBack({
      
    })
  },
  //nav栏点击
  selectNav(e) {
    var id = e.currentTarget.dataset.id;
    this.setData({
      navId: id
    })

    if (id == 0) {
      this.getList(1)
    } else if (id == 1) {
      this.getTopicList(1);
    }

  },
  showAllText(e) {
    var list = this.data.list;
    var pid = e.currentTarget.dataset.pid;
    list.some(item => {
      if (item.pid == pid) {
        item.showText = !item.showText;
        return true
      }
    })
    this.setData({
      list
      // showText: !this.data.showText
    })
  },

  //编辑我的资料
  editMyMeans(){
    wx.navigateTo({
      url: '/pages/editProfile/editProfile',
    })
  },

  //跳转动态详情
  toDetails(e){
    var pid=e.currentTarget.dataset.pid;
    wx.navigateTo({
      url: `/pages/dynamicDetail/dynamicDetail?pid=${pid}&type=1`
    })
  },

  //跳转话题详情
  toTopicDetails(e){
    var tid=e.currentTarget.dataset.tid;
    wx.navigateTo({
      url: `/pages/topicDetail/topicDetail?tid=${tid}`
    })
  },

  //详细资料
  toMeanDetail(){
    var eid=this.data.eid;
    wx.navigateTo({
      url: "/pages/meansDetail/meansDetail?eid="+eid
    })
  },
  //图片预览
  previewImg(e) {
    var imgs = e.currentTarget.dataset.imgs,
      img = e.currentTarget.dataset.img;
    console.log(imgs, img)
    wx.previewImage({
      current: img, // 当前显示图片的http链接
      urls: imgs // 需要预览的图片http链接列表
    })
  },

  getInfor(){
    var uid = app.globalData.uid;
    var eid=this.data.eid;
    http.fetchApi('procedure_t', { p: `R2019002|${eid}|${uid}` }).then(res=>{
      if(res.data.code==1){
        var infor = res.data.results[0];
        this.setData({
          infor
        })

        this.getList(1);
      }
    })
  },

  //关注
  addFollow() {
    var uid = app.globalData.uid;
    var p_uid = this.data.eid
    http.fetchApi('procedure_t', { p: `R2017010|${uid}|${p_uid}` }).then(res => {
      if (res.data.code == 1) {
        if (res.data.results[0].codes == 1) {
          wx.showToast({
            title: '已关注'
          })
          setTimeout(() => { this.getInfor();},1000)
          
        } else if (res.data.results[0].codes == 0) {
          wx.showToast({ title: '不能关注自己!', icon: 'none' })
        }
      }
    })
  },

  //取消关注
  cancelFollow(e){
    var uid = app.globalData.uid;
    var p_uid = this.data.eid
    http.fetchApi('procedure_t', { p: `R2018001|${uid}|${p_uid}` }).then(res => {
      if (res.data.code == 1) {
        wx.showToast({
          title: '取消关注',
        })
        setTimeout(() => {
          this.getInfor();
        }, 1000)
      }
    })
  },

  //获取我的动态列表信息
  getList(page) {
    var eid = this.data.eid;
    wx.showLoading({
      title: '加载中',
    })
    var uid = app.globalData.uid;
    http.fetchApi('procedure_t', { p: `R2018006|${eid}|${page}|10` }).then(res => {
      wx.hideLoading();
      var list = this.data.list;
      var list1 = res.data.results;
      if (page == 1) {
        list = res.data.results;
      } else {
        list = [...list, ...list1]
      }
      if (list1.length == 0 && page > 1) {
        http.showToast('暂无更多!' + '', '', 'none')
        return
      }
      list.forEach(item => {
        item.showText = false
      })
      this.setData({
        list,
        page
      })
    })
  },

  //获取我的话题列表
  getTopicList(page1) {
    wx.showLoading({
      title: '加载中',
    })
    var eid = this.data.eid;
    http.fetchApi('procedure_t', { p: `R2018007|${eid}|${page1}|10` }).then(res => {
      wx.hideLoading();
      var topicList = this.data.topicList;
      var topicList1 = res.data.results;

      if (page1 == 1) {
        topicList = res.data.results;
      } else {
        topicList = [...topicList, ...topicList1]
      }
      console.log(topicList)
      if (topicList1.length == 0 && page1 > 1) {
        http.showToast('暂无更多!' + '', '', 'none')
        return
      }
      this.setData({
        topicList,
        page1
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var eid=options.eid;
    if(eid==app.globalData.uid){
      this.setData({
        isMe:true
      })
    }
    this.setData({
      eid
    })
    this.getInfor();
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.navId == 0) {
      var page = this.data.page || 1;
      page++;
      this.getList(page);
    } else if (this.data.navId == 1) {
      var page1 = this.data.page1 || 1;
      page1++;
      this.getTopicList(page1);
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})